package com.zdxf.client.controller;

import com.zdxf.client.domain.LogDO;
import com.zdxf.client.domain.XmlDO;
import com.zdxf.client.util.XmlUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


@RestController
@RequestMapping("/xml")
public class XmlController {

    @PostMapping(value = "/getXmlFile")
    public Object getXmlFile(@RequestBody XmlDO xmlDO) throws Exception {
        String filePath = xmlDO.getPath();
        Document document = XmlUtil.getDocument(filePath);
        String docXmlText = document.asXML();//输出xml字符串
        return docXmlText;
    }

    @PostMapping(value = "/putXmlFile")
    public Object putXmlFile(@RequestBody XmlDO body, HttpServletResponse response) throws Exception {
        String filePath = body.getPath();
        Document document = updateXML(filePath, body.getName(),body.getValue());
        OutputFormat format = OutputFormat.createPrettyPrint();
        File xmlFile = new File(filePath);
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile), format);
        writer.write(document);
        writer.close();
        return "success";
    }

    public static Document updateXML(String filePath, String key, String value) throws Exception {
        Document document = getDocument(filePath);
        boolean flag = false;
        if (document != null) {
            // 修改指定节点
            List elementList = document.selectNodes("/configuration/property");
            for (int i = 0; i < elementList.size(); i++) {
                Element element = (Element) elementList.get(i);
                Element nameElement = element.element("name");
                Element valueElement = element.element("value");
                if (nameElement.getText().equals(key)) {
                    valueElement.setText(value);
                    flag = true;
                }

            }
            if (!flag) {
                Element rootElement = document.getRootElement();
                Element parentElement = rootElement.addElement("property");
                parentElement.addElement("name").setText(key);
                parentElement.addElement("value").setText(value);
            }
        }
        return document;
    }

    public static Document getDocument(String filename) {
        File xmlFile = new File(filename);
        Document document = null;
        if (xmlFile.exists()) {
            try {
                SAXReader saxReader = new SAXReader();
                document = saxReader.read(xmlFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return document;
    }

}
